package com.jeecg.struts.util;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.jeecg.struts.vo.StrutsBean;

public class ParserXml {

	
	public static void main(String[] args) {		
		
		Map<String, StrutsBean> map = readStrutsXml();
		Set<String> keySet = map.keySet();
		for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			StrutsBean strutsBean = map.get(key);
			System.out.println("path:"+key+"---"+strutsBean.toString());
		}
		
	}
	
	public static  void test(InputStream  input){
		SAXBuilder  sb = new SAXBuilder();
		try {
			
			Document build = sb.build(input);
			//Document build = sb.build(new File("WebContent/WEB-INF/struts-config.xml"));
			Element rootElement = build.getRootElement();
			Element formbeans = rootElement.getChild("form-beans");
			List<Element> formbeanList = formbeans.getChildren();
			for (Element element:formbeanList ) {
				 String name = element.getAttribute("name").getValue();
				 String formClass = element.getAttribute("form-class").getValue();
				 System.out.println("name:"+name +" formClass:"+formClass);
			}
			
			Element actionMappings = rootElement.getChild("action-mappings");
			List<Element> actionList = actionMappings.getChildren();
			for (Element element:actionList ) {
				 String name = element.getAttribute("name").getValue();
				 String actionClass = element.getAttribute("action-class").getValue();
				 String path = element.getAttribute("path").getValue();
				 System.out.println("name:"+name +"--actionClass:"+actionClass +"--path:"+path );
				 List<Element> forwardList = element.getChildren();
				 for (Element forward : forwardList) {
					  String resultKey = forward.getAttribute("name").getValue();
					  String resultValue = forward.getAttribute("value").getValue();
					  System.out.println("resultKey:"+resultKey +"--resultValue:"+resultValue );
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static  Map <String,StrutsBean>  readStrutsXml(InputStream  input){
		SAXBuilder  sb = new SAXBuilder();
		Map <String,StrutsBean> strutsMap = new HashMap<String, StrutsBean>();
		try {
			Document build = sb.build(input);
			Element rootElement = build.getRootElement();
			Element formbeans = rootElement.getChild("form-beans");
			List<Element> formbeanList = formbeans.getChildren();
			Element actionMappings = rootElement.getChild("action-mappings");
			List<Element> actionList = actionMappings.getChildren();
			
			for (Element element:actionList ) {
				StrutsBean strutsBean = new StrutsBean();
				 String actionName = element.getAttribute("name").getValue();
				 String actionClass = element.getAttribute("action-class").getValue();
				 String path = element.getAttribute("path").getValue();
				 List<Element> forwardList = element.getChildren();
				 Map<String,String > actionForword = new HashMap<String,String >();
				 for (Element forward : forwardList) {
					  String resultKey = forward.getAttribute("name").getValue();
					  String resultValue = forward.getAttribute("value").getValue();
					  actionForword.put(resultKey, resultValue);			  
				 }
				 strutsBean.setActionName(actionName);
				 strutsBean.setActionClass(actionClass);
				 strutsBean.setActionForword(actionForword) ;
				 for (Element formbeanEle:formbeanList ) {
					 String formName = formbeanEle.getAttribute("name").getValue();
					 String formClass = formbeanEle.getAttribute("form-class").getValue();
					 if(actionName.equals(formName)){
						 strutsBean.setFormName(formName);
						 strutsBean.setFormClass(formClass);
					 }
				 }
				 strutsMap.put(path, strutsBean);
			}
			return strutsMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strutsMap;
		
	}
	
	
	public static  Map <String,StrutsBean>  readStrutsXml(String xmlPaht){
		SAXBuilder  sb = new SAXBuilder();
		Map <String,StrutsBean> strutsMap = new HashMap<String, StrutsBean>();
		try {
			Document build = sb.build(new File(xmlPaht));
			Element rootElement = build.getRootElement();
			Element formbeans = rootElement.getChild("form-beans");
			List<Element> formbeanList = formbeans.getChildren();
			Element actionMappings = rootElement.getChild("action-mappings");
			List<Element> actionList = actionMappings.getChildren();
			
			for (Element element:actionList ) {
				StrutsBean strutsBean = new StrutsBean();
				 String actionName = element.getAttribute("name").getValue();
				 String actionClass = element.getAttribute("action-class").getValue();
				 String path = element.getAttribute("path").getValue();
				 List<Element> forwardList = element.getChildren();
				 Map<String,String > actionForword = new HashMap<String,String >();
				 for (Element forward : forwardList) {
					  String resultKey = forward.getAttribute("name").getValue();
					  String resultValue = forward.getAttribute("value").getValue();
					  actionForword.put(resultKey, resultValue);			  
				 }
				 strutsBean.setActionName(actionName);
				 strutsBean.setActionClass(actionClass);
				 strutsBean.setActionForword(actionForword) ;
				 for (Element formbeanEle:formbeanList ) {
					 String formName = formbeanEle.getAttribute("name").getValue();
					 String formClass = formbeanEle.getAttribute("form-class").getValue();
					 if(actionName.equals(formName)){
						 strutsBean.setFormName(formName);
						 strutsBean.setFormClass(formClass);
					 }
				 }
				 strutsMap.put(path, strutsBean);
			}
			return strutsMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strutsMap;
		
	}
	
	
	
	
	
	public static  Map <String,StrutsBean>  readStrutsXml(){
		SAXBuilder  sb = new SAXBuilder();
		Map <String,StrutsBean> strutsMap = new HashMap<String, StrutsBean>();
		try {
			Document build = sb.build(new File("WebContent/WEB-INF/struts-config.xml"));
			Element rootElement = build.getRootElement();
			Element formbeans = rootElement.getChild("form-beans");
			List<Element> formbeanList = formbeans.getChildren();
			Element actionMappings = rootElement.getChild("action-mappings");
			List<Element> actionList = actionMappings.getChildren();
			
			for (Element element:actionList ) {
				StrutsBean strutsBean = new StrutsBean();
				 String actionName = element.getAttribute("name").getValue();
				 String actionClass = element.getAttribute("action-class").getValue();
				 String path = element.getAttribute("path").getValue();
				 List<Element> forwardList = element.getChildren();
				 Map<String,String > actionForword = new HashMap<String,String >();
				 for (Element forward : forwardList) {
					  String resultKey = forward.getAttribute("name").getValue();
					  String resultValue = forward.getAttribute("value").getValue();
					  actionForword.put(resultKey, resultValue);			  
				 }
				 strutsBean.setActionName(actionName);
				 strutsBean.setActionClass(actionClass);
				 strutsBean.setActionForword(actionForword) ;
				 for (Element formbeanEle:formbeanList ) {
					 String formName = formbeanEle.getAttribute("name").getValue();
					 String formClass = formbeanEle.getAttribute("form-class").getValue();
					 if(actionName.equals(formName)){
						 strutsBean.setFormName(formName);
						 strutsBean.setFormClass(formClass);
					 }
				 }
				 strutsMap.put(path, strutsBean);
			}
			return strutsMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strutsMap;
		
	}
	
	
	
	
	
}










